package com.fzx.aidlclient;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;

import com.fzx.aidlservice.IAidlInterface;
import com.fzx.aidlservice.bean.UserInfo;
import com.fzx.aidlservice.callback.ICallBack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static String TAG = "fzx";
    private TextView tv_result;
    private GridLayout grid_layout;
    private ServiceManager serviceManager;
    private IAidlInterface iAidlInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        serviceManager = ServiceManager.getInstance(getApplicationContext());
        Button btn_bind = findViewById(R.id.btn_bind);
        Button btn_unbind = findViewById(R.id.btn_unbind);
        Button btn_clear_service = findViewById(R.id.btn_clear_service);
        btn_bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "MainActivity onClick: 正在绑定服务");
                tv_result.setText("正在绑定服务");
                serviceManager.bindService(new ServiceManager.OnServiceCallback() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        tv_result.setText("服务绑定成功");
                        Log.i(TAG, "onServiceConnected: 服务绑定成功");
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {
                        tv_result.setText("服务绑定失败");
                        Log.i(TAG, "onServiceDisconnected: 服务绑定失败");
                    }
                });
            }
        });
        btn_unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                serviceManager.unBindService();
                tv_result.setText("解除绑定服务");
                Log.i(TAG, "MainActivity onClick: 解除绑定服务");
            }
        });
        btn_clear_service.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!serviceManager.isServiceBind()) {
                    tv_result.setText("服务未绑定");
                    return;
                }
                if (iAidlInterface == null) {
                    iAidlInterface = serviceManager.getIAidlInterface();
                }
                tv_result.setText("");
                try {
                    iAidlInterface.clearServiceData();
                    tv_result.append("服务执行清空数据\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
            }
        });
        grid_layout = findViewById(R.id.grid_layout);
        tv_result = findViewById(R.id.tv_result);
        for (int i = 0; i < grid_layout.getChildCount(); i++) {
            final int finalI = i;
            Button button = (Button) grid_layout.getChildAt(i);
//            final String s = button.getText().toString();
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    tv_result.setText("发起通讯：" + s);
                    setItemClick(finalI);
                }
            });
        }
    }

    private void bindServiceTest() {
        Intent intent = new Intent();
        intent.setAction("com.fzx.aidlclient.MyService");
        intent.setPackage("com.fzx.aidlclient");
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.i(TAG, "onServiceConnected: ");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.i(TAG, "onServiceDisconnected: ");
            }
        }, BIND_AUTO_CREATE);

    }

    private void setItemClick(int position) {
        if (!serviceManager.isServiceBind()) {
            tv_result.setText("服务未绑定");
            return;
        }
        if (iAidlInterface == null) {
            iAidlInterface = serviceManager.getIAidlInterface();
        }
        tv_result.setText("");
        Button button = (Button) grid_layout.getChildAt(position);
        tv_result.append("发起通讯：" + button.getText().toString() + "\n");
        switch (position) {
            case 0://基本类型int ,short , long , byte , char , Boolean , float ,double
                try {
                    int add = iAidlInterface.add(12, 14);
                    tv_result.append("服务返回数据:" + add + "\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 1://String类型
                try {
                    String res = iAidlInterface.getString("客户端传入String数据");
                    tv_result.append("服务返回数据:" + res + "\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 2://CharSequence类型
                try {
                    CharSequence res = iAidlInterface.getCharSequence("客户端传入CharSequence数据");
                    tv_result.append("服务返回数据:" + res.toString() + "\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 3://Void类型，进入设置
                try {
                    iAidlInterface.gotoSetting();
                    tv_result.append("服务执行打开设置\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 4://自定义入参
                try {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserName("fangzhixin");
                    userInfo.setPassword("123456");
                    userInfo.setUserId(19);
                    userInfo.setId(11);
                    userInfo.setId2(22);
                    userInfo.setId3(33);
                    iAidlInterface.setUserInfo(userInfo);
                    tv_result.append("服务执行设置UserInfo\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 5://自定义类型
                try {
                    UserInfo info = iAidlInterface.getUserInfo();
                    if (info != null) {
                        tv_result.append("服务返回数据:" + info.toString() + "\n");
                    } else {
                        tv_result.append("服务返回数据:" + null + "\n");
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 6://数组入参
                try {
                    iAidlInterface.setArrayData(new String[]{"12", "34", "56"});
                    tv_result.append("服务执行设置数组\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 7://数组
                try {
                    String[] arrayData = iAidlInterface.getArrayData();
                    if (arrayData != null) {
                        tv_result.append("服务返回数据:" + Arrays.toString(arrayData) + "\n");
                    } else {
                        tv_result.append("服务返回数据:" + null + "\n");
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 8://基本List入参
                try {
                    List<String> list = new ArrayList<>();
                    list.add("基本List数据0");
                    list.add("基本List数据1");
                    list.add("基本List数据2");
                    iAidlInterface.setDataList(list);
                    tv_result.append("服务执行设置基本List数据\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 9://基本List集合
                try {
                    List<String> dataList = iAidlInterface.getDataList();
                    if (dataList == null) {
                        tv_result.append("服务返回数据:" + null + "\n");
                    } else {
                        tv_result.append("服务返回数据:" + dataList.toString() + "\n");
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 10://复杂List入参
                try {
                    List<UserInfo> userList = new ArrayList<>();
                    userList.add(new UserInfo("fzx11", "11111", 123));
                    userList.add(new UserInfo("fzx22", "22222", 456));
                    userList.add(new UserInfo("fzx33", "33333", 789));
                    iAidlInterface.setUserList(userList);
                    tv_result.append("服务执行设置复杂List数据\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 11://复杂List集合
                try {
                    List<UserInfo> userList = iAidlInterface.getUserList();
                    if (userList == null) {
                        tv_result.append("服务返回数据:" + null + "\n");
                    } else {
                        tv_result.append("服务返回数据:" + userList.toString() + "\n");
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 12://map入参
                try {
                    HashMap<String, UserInfo> map = new HashMap<>();
                    map.put("name", new UserInfo("fangzhixin1", "11111", 11));
                    map.put("pass", new UserInfo("fangzhixin2", "22222", 22));
                    iAidlInterface.setMap(map);
                    tv_result.append("服务执行设置Map数据\n");
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 13://map集合
                try {
                    HashMap<String, UserInfo> map = (HashMap<String, UserInfo>) iAidlInterface.getMap();
                    if (map == null) {
                        tv_result.append("服务返回数据:" + null + "\n");
                    } else {
                        tv_result.append("服务返回数据:" + map.toString() + "\n");
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 14://回调
                try {
                    iAidlInterface.testCallback(new ICallBack.Stub() {
                        @Override
                        public void onSuccess(String info) throws RemoteException {
                            tv_result.append("服务返回数据:" + info + "\n");
                        }

                        @Override
                        public void onError() throws RemoteException {

                        }
                    });
                } catch (RemoteException e) {
                    e.printStackTrace();
                    tv_result.setText("服务异常");
                }
                break;
            case 15://指标in
                UserInfo in = new UserInfo();
                in.setUserId(1);
                in.setUserName("client.fzx.in");
                in.setPassword("client.in11111111");
                tv_result.append("客户端发送in：" + in.toString() + "\n");
                Log.i(TAG, "客户端发送in: " + in.toString());
                try {
                    iAidlInterface.testin(in);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                tv_result.append("客户端UserInfo：in=" + in.toString() + "\n");
                Log.i(TAG, "客户端result: " + in.toString());
                break;
            case 16://指标out
                UserInfo out = new UserInfo();
                out.setUserId(2);
                out.setUserName("client.fzx.out");
                out.setPassword("client.out22222222");
                tv_result.append("客户端发送out：" + out.toString() + "\n");
                Log.i(TAG, "客户端发送out: " + out.toString());
                try {
                    iAidlInterface.testout(out);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                tv_result.append("客户端UserInfo：out=" + out.toString() + "\n");
                Log.i(TAG, "客户端result: " + out.toString());
                break;
            case 17://指标inout
                UserInfo inout = new UserInfo();
                inout.setUserId(3);
                inout.setUserName("client.fzx.inout");
                inout.setPassword("client.inout33333333");
                tv_result.append("客户端发送inout：" + inout.toString() + "\n");
                Log.i(TAG, "客户端发送inout: " + inout.toString());
                try {
                    iAidlInterface.testinout(inout);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                tv_result.append("客户端UserInfo：inout=" + inout.toString() + "\n");
                Log.i(TAG, "客户端result: " + inout.toString());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
